﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LetiLib.Data;
using DevExpress.XtraBars.Ribbon;
using DevExpress.Utils;
using DevExpress.Utils.Drawing;

namespace LedCenter.GUI.PopupControls
{
    public abstract partial class LedPopUpControl : UserControl
    {
        public LedPopUpControl()
        {
            InitializeComponent();
        }

        ImageList imgList = new ImageList();
        public LedPopUpControl(POPUP p,bool editMode = false)
        {
            InitializeComponent();
            
            txtX.Enabled = isUpdateMode;
            txtY.Enabled = isUpdateMode;
            txtZ.Enabled = isUpdateMode;
            txtWidth.Enabled = isUpdateMode;
            txtHeight.Enabled = isUpdateMode;
            cbType.Enabled = isUpdateMode;

            gcPlaylist.Enabled = true;
            imageDetails1.Enabled = false;
            btnCancel.Visible = btnOK.Visible = editMode;
            _popup = p;
            cbType.DataSource = Enum.GetValues(typeof(POPUP_TYPE));
            
            txtX.Text = p.X.ToString();
            txtY.Text = p.Y.ToString();
            txtZ.Text = p.Z.ToString();
            txtWidth.Text = p.Width.ToString();
            txtHeight.Text = p.Height.ToString();
            cbType.SelectedIndex = Array.IndexOf(Enum.GetValues(typeof(POPUP_TYPE)), p.DataType);
            
            gcPlaylist.Gallery.ItemImageLayout = ImageLayoutMode.ZoomInside;
            gcPlaylist.Gallery.ImageSize = new Size(120, 120);
            gcPlaylist.Gallery.ShowItemText = true;
            gcPlaylist.Gallery.ShowGroupCaption = true;

            GalleryItemGroup group1 = new GalleryItemGroup();
            group1.Caption = "Images";
            gcPlaylist.Gallery.Groups.Add(group1);
            
            if (p.DataType == POPUP_TYPE.PLAYLIST)
            {
                gcPlaylist.Visible = true;
                flowLayoutPanel1.Visible = true;
                imageDetails1.Visible = true;
                busControl1.Visible = false;
                populateGallery();
            }
            else if (p.DataType == POPUP_TYPE.TABLE)
            {
                gcPlaylist.Visible = false;
                imageDetails1.Visible = false;
                flowLayoutPanel1.Visible = false;
                busControl1.Visible = true;
                busControl1.DataSource = (TABLE_POPUP)_popup;
                busControl1.BusUpdate += new BusControl.BusUpdateHandler(busControl1_BusUpdate);
                this.Height = this.MaximumSize.Height;
            }
            else
            {
                gcPlaylist.Visible = false;
                imageDetails1.Visible = false;
                busControl1.Visible = false;
            }

            imageDetails1.btnOK.Click +=new EventHandler(imgDetailBtnUpdate_Click);
        }

        private void populateGallery()
        {
            imgList.Images.Clear();
            gcPlaylist.Gallery.Groups[0].Items.Clear();
            int index = 0;
            
            if (((PLAYLIST_POPUP)_popup).Images != null && ((PLAYLIST_POPUP)_popup).Images.Count > 0)
            {
                string basePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                foreach (IMAGE img in ((PLAYLIST_POPUP)_popup).Images)
                {
                    string path = basePath + "\\DB\\Images\\" + img.ImageCode + ".png";
                    Image sourceBitmap;
                    if (System.IO.File.Exists(path))
                    {
                        sourceBitmap = Image.FromFile(path);
                    }
                    else
                    {
                        sourceBitmap = new Bitmap(120, 120);
                        Graphics g = Graphics.FromImage(sourceBitmap);
                        g.Clear(Color.Red);
                    }

                    gcPlaylist.Gallery.Groups[0].Items.Add(new GalleryItem(sourceBitmap, img.PlayOrder.ToString(), "Description"));
                    index++;
                }

            }
            this.Height = this.MaximumSize.Height;
        }

        ushort selectedImageIndex = 0;
        private void gcPlaylist_Gallery_ItemClick(object sender, GalleryItemClickEventArgs e)
        {
            if (((PLAYLIST_POPUP)_popup).Images != null && ((PLAYLIST_POPUP)_popup).Images.Count > 0)
            {
                selectedImageIndex = 0;
                btnDeleteImage.Enabled = true;

                foreach (IMAGE img in ((PLAYLIST_POPUP)_popup).Images)
                {
                    if (img.PlayOrder.ToString().Equals(e.Item.Caption))
                    {
                        imageDetails1.DataSource = img;
                        imageDetails1.Enabled = true;
                        break;
                    }
                    selectedImageIndex++;
                }
            }

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((POPUP_TYPE)Enum.Parse(typeof(POPUP_TYPE), cbType.SelectedValue.ToString()) == POPUP_TYPE.PLAYLIST)
            {
                this.Height = this.MaximumSize.Height;
            }
            else
            {
                this.Height = this.MinimumSize.Height;
            }
            
        }

        public POPUP _popup;

        private void btnOK_Click(object sender, EventArgs e)
        {
            //_popup.ID = 0;
            _popup.X = ushort.Parse(txtX.Text);
            _popup.Y = ushort.Parse(txtY.Text);
            _popup.Z = byte.Parse(txtZ.Text);
            _popup.Width = ushort.Parse(txtWidth.Text);
            _popup.Height = ushort.Parse(txtHeight.Text);
            _popup.DataType = (POPUP_TYPE)Enum.Parse(typeof(POPUP_TYPE), cbType.SelectedItem.ToString());
            _popup.StartDate = new DATETIME();
            _popup.EndDate = new DATETIME();
            //_popup.ItemCount = 0;
            //_popup.ActiveItem = 0;

            if (isUpdateMode)
            {
                if (OnPopupUpdate != null)
                    OnPopupUpdate(this);
            }
        }

        void busControl1_BusUpdate()
        {
            _popup = busControl1.DataSource;
            if (OnPopupUpdate != null)
                OnPopupUpdate(this);
        }

        bool isUpdateMode = false;
        public delegate void UpdatePopupHandler(object sender);
        public event UpdatePopupHandler OnPopupUpdate;
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            isUpdateMode = true;
            btnCancel.Visible = btnOK.Visible = isUpdateMode;
            txtX.Enabled = isUpdateMode;
            txtY.Enabled = isUpdateMode;
            txtZ.Enabled = isUpdateMode;
            txtWidth.Enabled = isUpdateMode;
            txtHeight.Enabled = isUpdateMode;
            cbType.Enabled = isUpdateMode;
            imageDetails1.isEdit = isUpdateMode;
            imageDetails1.btnOK.Text = "Update";
            gcPlaylist.Enabled = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            isUpdateMode = false;
            btnCancel.Visible = btnOK.Visible = isUpdateMode;

            txtX.Text = _popup.X.ToString();
            txtX.Enabled = isUpdateMode;
            txtY.Text = _popup.Y.ToString();
            txtY.Enabled = isUpdateMode;
            txtZ.Text = _popup.Z.ToString();
            txtZ.Enabled = isUpdateMode;
            txtWidth.Text = _popup.Width.ToString();
            txtWidth.Enabled = isUpdateMode;
            txtHeight.Text = _popup.Height.ToString();
            txtHeight.Enabled = isUpdateMode;
            cbType.SelectedItem = _popup.DataType;
            cbType.Enabled = isUpdateMode;
            imageDetails1.isEdit = isUpdateMode;
            gcPlaylist.Enabled = true;
            gcPlaylist.Gallery.ItemImageLayout = ImageLayoutMode.ZoomInside;
            gcPlaylist.Gallery.ImageSize = new Size(90, 90);
            gcPlaylist.Gallery.ShowItemText = true;
            gcPlaylist.Gallery.ShowGroupCaption = true;

            GalleryItemGroup group1 = new GalleryItemGroup();
            group1.Caption = "Images";
            gcPlaylist.Gallery.Groups.Clear();
            gcPlaylist.Gallery.Groups.Add(group1);
            string basePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            if (_popup.DataType == POPUP_TYPE.PLAYLIST)
            {
                imgList.Images.Clear();
                int index = 0;
                if (((PLAYLIST_POPUP)_popup).Images != null)
                {
                    foreach (IMAGE img in ((PLAYLIST_POPUP)_popup).Images)
                    {
                        string path = basePath + "\\DB\\Images\\" + img.ImageCode + ".png";
                        Image sourceBitmap;
                        if (System.IO.File.Exists(path))
                        {
                            sourceBitmap = Image.FromFile(path);
                        }
                        else
                        {
                            sourceBitmap = new Bitmap(90, 90);
                            Graphics g = Graphics.FromImage(sourceBitmap);
                            g.Clear(Color.Red);
                        }

                        group1.Items.Add(new GalleryItem(sourceBitmap, img.PlayOrder.ToString(), "Description"));
                        index++;
                    }
                }

            }
        }

        public delegate void AddImageToPlaylistHandler(object sender);
        public event AddImageToPlaylistHandler OnImageAdd;
        private void addImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ushort imgPO = 0;// play order

            bool foundID = false;
            for (imgPO = 0; imgPO < ((PLAYLIST_POPUP)_popup).Images.Count; imgPO++)
            {
                foreach (IMAGE p in ((PLAYLIST_POPUP)_popup).Images)
                {
                    if (p.PlayOrder == imgPO)
                    {
                        foundID = true;
                        break;
                    }
                    foundID = false;
                }
                if (foundID)
                    continue;
                break;
            }

            Form form1 = new Form();
            form1.ControlBox = false;
            form1.Text = "Add image";
            form1.MinimumSize = form1.MaximumSize = new System.Drawing.Size(220, 240);
            form1.Size = new System.Drawing.Size(220, 240);
            form1.StartPosition = FormStartPosition.CenterParent;
            ImageDetails imgDt = new ImageDetails(imgPO);
            imgDt.Dock = DockStyle.Fill;
            form1.Controls.Add(imgDt);
            DialogResult dr = form1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                ((PLAYLIST_POPUP)_popup).Images.Add(imgDt.DataSource);
            }

            populateGallery();
            // add event here
        }
        private void btnAddImage_Click(object sender, EventArgs e)
        {
            ushort imgPO = 0;// play order
            if (((PLAYLIST_POPUP)_popup).Images == null)
                ((PLAYLIST_POPUP)_popup).Images = new List<IMAGE>();

            bool foundID = false;
            for (imgPO = 0; imgPO < ((PLAYLIST_POPUP)_popup).Images.Count; imgPO++)
            {
                foreach (IMAGE p in ((PLAYLIST_POPUP)_popup).Images)
                {
                    if (p.PlayOrder == imgPO)
                    {
                        foundID = true;
                        break;
                    }
                    foundID = false;
                }
                if (foundID)
                    continue;
                break;
            }

            Form form1 = new Form();
            form1.ControlBox = false;
            form1.Text = "Add image";
            //form1.MinimumSize = form1.MaximumSize = new System.Drawing.Size(220, 240);
            form1.Size = new System.Drawing.Size(220, 240);
            form1.StartPosition = FormStartPosition.CenterParent;
            ImageDetails imgDt = new ImageDetails(imgPO);
            imgDt.isEdit = true;
            imgDt.Dock = DockStyle.Fill;
            form1.Controls.Add(imgDt);
            DialogResult dr = form1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                ((PLAYLIST_POPUP)_popup).Images.Add(imgDt.DataSource);
                _popup.ItemCount++;
            }

            populateGallery();
            //TODO: add event here
            if (OnImageAdd != null)
            {
                OnImageAdd(this);
            }
        }

        public delegate void DeleteImageFromPlaylistHandler(object sender,byte playOrder);
        public event DeleteImageFromPlaylistHandler OnImageDelete;
        private void btnDeleteImage_Click(object sender, EventArgs e)
        {
            if (((PLAYLIST_POPUP)_popup).Images.Count <= 1)
                MessageBox.Show("Can't delete this image, playlist must has at least one image!");

            if (selectedImageIndex > 0)
            {
                ((PLAYLIST_POPUP)_popup).Images.RemoveAt(selectedImageIndex);
                populateGallery();
            }
            btnDeleteImage.Enabled = false;
            // TODO: add delete event here
            //TODO: add event here
            if (OnImageDelete != null)
            {
                OnImageDelete(this, (byte)selectedImageIndex);
            }
        }

        public delegate void UpdateImageHandler(object sender, byte playOrder);
        public event UpdateImageHandler OnImageUpdate;
        private void imgDetailBtnUpdate_Click(object sender, EventArgs e)
        {
            ((PLAYLIST_POPUP)_popup).Images[selectedImageIndex] = imageDetails1.DataSource;
            imageDetails1.Visible = true;
            imageDetails1.Enabled = false;
            if (OnImageUpdate != null)
                OnImageUpdate(this, (byte)selectedImageIndex);
        }

        
    }
}
